import Vuex from 'vuex'
import Vue from 'vue'
import {HttpManager} from "@/api";

/**
 * 组件化使用vuex的store
 *
 * */
const module1={
    namespaced:true,
    state:{
        tasks: [],
        todayTasks:[],
        habits:[]
    },
    mutations: {
        TASKS(state,value) {
            state.tasks = value
        },
        TODAYTASKS(state,value){
            state.todayTasks = value
        },
        HABITS(state,value){
            state.habits = value
        }
    },
    actions:{
        getTasks(context,value){
            /*HttpManager.getAllTasks(value).then((response)=>{
                context.commit('TASKS',response)
            })*/
            let it = new URLSearchParams()
            it.append('userId',value)
            HttpManager.getAllTasks(it).then((response)=>{
                /*for (let item of response) {
                  tasks.push(item)
                }*/
                context.commit('TASKS',response)
            })
        },
        getTodayTasks(context,value){
            let it = new URLSearchParams()
            it.append('userId',value.userId)
            it.append('n',value.n)
            HttpManager.getTasksByParam(it).then((response)=>{
                /*for (let item of response) {
                  tasks.push(item)
                }*/
                context.commit('TODAYTASKS',response)
            })
        },
        addTasks(context,value){
            let tasks = context.state.tasks
            tasks.push(value)
            context.commit('TASKS',tasks)
        },
        addTodayTasks(context,value){
            let tasks = context.state.todayTasks
            tasks.push(value)
            context.commit('TODAYTASKS',tasks)
        },
        deleteTask(context,value){
            let tasks = context.state.tasks
            tasks.splice(value,1)
            context.commit('TASKS',tasks)
        },
        deleteTodayTask(context,value){
            let tasks = context.state.todayTasks
            tasks.splice(value,1)
            context.commit('TODAYTASKS',tasks)
        },
        getHabits(context,value){
            let it = new  URLSearchParams()
            it.append('userId',value)
            HttpManager.getHabitsById(it).then((response)=>{
                context.commit('HABITS',response)
            })
        },
        addHabit(context,value){
            let tasks = context.state.habits
            tasks.push(value)
            context.commit('HABITS',tasks)
        }
    }

}

const module2 ={
    namespaced: true,
    state:{
        signs:[]
    },
    mutations: {
        SIGNS(state,value){
            state.signs = value
        }
    },
    actions: {
        getSigns(context,value){
            let it = new  URLSearchParams()
            it.append('hTaskId',value)
            HttpManager.getSigns(it).then((response)=>{
                context.commit('SIGNS',response)
            })
        },
        addSign(context,value){
            let tasks = context.state.signs
            tasks.push(value)
            context.commit('SIGNS',tasks)
        }

    }
}

Vue.use(Vuex);
export default new Vuex.Store({
    modules:{
        module1,
        module2
    }
})
